package db;

import java.sql.*;

/**
 * @author 4ui
 */
public class DB {
    private String  url = "jdbc:mysql://localhost:3306/test", user = "root", password = "1";
    private Statement st; 
    private Connection conn;
    private ResultSet rs;
    private int numbers;
    private String words;
    
    public DB() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            DriverManager.registerDriver((Driver)Class.forName("com.mysql.jdbc.Driver").newInstance());
        }
        catch (Exception e) {
            System.out.println("Register driver: "+e);
        }
    }
    
    String getName(int id) {
        /*
         * Волшебный клас получения name из базы
         */
        try {
            //запрос в БД
            conn = DriverManager.getConnection(url,user,password);
            st = conn.createStatement();
            rs=st.executeQuery("SELECT name FROM twoplans WHERE id =" +id);
            while (rs.next()) {
                words=rs.getString("name");
            }
            st.close();
            conn.close();
        }
        catch (Exception e){
            System.out.println(e);
        }
        return words;
    }
    
    int getKills(int id) {
        /*
         * Волшебный клас получения kills из базы
         */
        try {
            //запрос в БД
            conn = DriverManager.getConnection(url,user,password);
            st = conn.createStatement();
            rs=st.executeQuery("SELECT kills FROM twoplans WHERE id = " +id);
            while (rs.next()) {
                numbers=rs.getInt("kills");
            }
            st.close();
            conn.close();
        }
        catch (Exception e){
            System.out.println(e);
        }
        return numbers;
    }
    
    int getDeaths(int id) {
        /*
         * Волшебный клас получения deaths из базы
         */
        try {
            //запрос в БД
            conn = DriverManager.getConnection(url,user,password);
            st = conn.createStatement();            
            rs=st.executeQuery("SELECT deaths FROM twoplans WHERE id = " +id);
            int count=0;
            while (rs.next()) {
                numbers=rs.getInt("deaths");
                count++;
            }
            st.close();
            conn.close();
        }
        catch (Exception e){
            System.out.println(e);
        }
        return numbers;
    }
    
    public static void main(String[] args) {
        DB twoplansDB = new DB();
        System.out.println(twoplansDB.getName(2));
    }
}
